package com.uchengwang.ui.base_ui;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;
import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.zhy.autolayout.utils.AutoUtils;


public class ActivityTitleBar extends RelativeLayout {

    private TextView tvTitle, rightTV;
    private TextView leftTextView;
    private Button btRight1;//标题栏右变的button控件 从左右数第一个
    private Button btRight2;//标题栏右变的button控件 从左右数第二个
    private ImageView imageRight;//标题栏右边的图片控件
    private RelativeLayout centreView, titleBar_bg2;
    private LinearLayout contentView;
    private OnClickListener btRight1Listener;
    private ImageView centerImage;//fresco显示图片控件
    private ImageView leftView, imageLeft_iv;//左边的图片控件
    private LinearLayout rl_left;//左边的布局
    private TextView msg_noread, line;
    private ImageView titleBar_image;
    private MsgView msg_num_text;

    public ActivityTitleBar(Context context) {
        this(context, null);
    }

    public ActivityTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActivityTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        contentView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.title_bar, null);
        titleBar_bg2 = (RelativeLayout) contentView.findViewById(R.id.titleBar_bg2);
        titleBar_image = (ImageView) contentView.findViewById(R.id.titleBar_image);
        leftTextView = (TextView) contentView.findViewById(R.id.leftTextView);
        tvTitle = (TextView) contentView.findViewById(R.id.title_bar_tvTitle);
        centreView = (RelativeLayout) contentView.findViewById(R.id.title_center_view);
        btRight1 = (Button) contentView.findViewById(R.id.btRight1);
        btRight2 = (Button) contentView.findViewById(R.id.btRight2);
        imageRight = (ImageView) contentView.findViewById(R.id.imageRight);
        rightTV = (TextView) contentView.findViewById(R.id.rightTV);
        centerImage = (ImageView) contentView.findViewById(R.id.title_image);
        leftView = (ImageView) contentView.findViewById(R.id.leftView);
        rl_left = (LinearLayout) contentView.findViewById(R.id.rl_left);
        msg_noread = (TextView) contentView.findViewById(R.id.msg_noread);
        imageLeft_iv = (ImageView) contentView.findViewById(R.id.imageLeft_iv);

        line = (TextView) contentView.findViewById(R.id.title_bar_line);

        msg_num_text = contentView.findViewById(R.id.msg_num_text);
//        ImageManager.getInstance().loadImage(context, line, R.drawable.image_line, ImageManager.ImageType.NARMOL);
//        line.setImageResource(R.drawable.image_line);

        getBackView();
        addView(contentView);
    }

    //给titleBar设置背景颜色
    public void setTitleBarBackground(int color) {
        titleBar_bg2.setBackgroundResource(color);
        titleBar_image.setVisibility(GONE);
    }

    public RelativeLayout getTitleBar_bg(){
        return titleBar_bg2;
    }

    /**
     *给titleBar设置背景图
     */
    public void setBg(int resId){
        ImageManager.getInstance().loadImage(getContext(), titleBar_image, resId, ImageManager.ImageType.NARMOL);
    }

    /**
     * 设置左边的未读消息
     * 显示的话 可以显示具体的数值  当num为0 表示显示一个圆点
     * */
    public void setLeftMsg(int visible, int num){
        msg_num_text.setVisibility(visible);
        if (View.VISIBLE == visible){
            UnreadMsgUtils.show(msg_num_text, num);
        }

    }



    public LinearLayout getRlLeft(){
        return rl_left;
    }

    public void visibleBottomLine(boolean visible) {
        if (visible){
            line.setVisibility(View.VISIBLE);
        } else{
            line.setVisibility(View.GONE);
        }
    }

    //默认左边的点击界面finish
    private void getBackView() {
        if (rl_left != null) {
            rl_left.setVisibility(View.VISIBLE);
        }
        if (getContext() instanceof Activity)
            rl_left.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) getContext()).finish();
                }
            });
    }

    /**
     * 左边设置点击事件，方便做返回提醒操作
     * @param listener 点击事件
     * */
    public void setLeftListener(OnClickListener listener){
        if (rl_left != null) {
            rl_left.setVisibility(View.VISIBLE);
            rl_left.setOnClickListener(listener);
        }
    }

    /**
     * @param title 设置左边文字
     * @param color 设置颜色
     * @param src 设置图片资源
     * @param onClickListener 设置点击事件 默认点击事件是finish当前界面
     * */
    public void setLeftView(String title, int color, int src, OnClickListener onClickListener) {
        if (TextUtils.isEmpty(title)){
            leftTextView.setVisibility(View.GONE);
        }else {
            leftTextView.setVisibility(View.VISIBLE);
            leftTextView.setText(title);
        }
        if (color !=0){
            leftTextView.setTextColor(getResources().getColor(color));
        }
        if (src != 0){
            leftView.setImageResource(src);
        }else {
            leftView.setVisibility(View.GONE);
        }

        if (onClickListener != null){
            rl_left.setOnClickListener(onClickListener);
        }
    }

    //控制返回键显示与否
    public void setLeftVisible(boolean isVisiable) {
        if (leftView != null) {
            if (isVisiable){
                leftView.setVisibility(View.VISIBLE);
            }else {
                leftView.setVisibility(View.GONE);
                rl_left.setEnabled(false);
            }
        }
    }

    /**
     * 控制leftView的控件宽高 与旋转角度
     * @param width 宽
     * @param height 高
     * @param res 旋转角度
     * @param listener 点击事件
     */
    public void setLeftViewWHA(int width, int height, int res , OnClickListener listener){
        if (leftTextView != null) {
            leftTextView.setVisibility(View.GONE);
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AutoUtils.getPercentWidthSize(width),
                AutoUtils.getPercentHeightSize(height));
        leftView.setLayoutParams(params);
        leftView.setImageResource(res);
//        leftView.setRotation(-90);
        if (listener != null){
            rl_left.setOnClickListener(listener);
        }

    }

    //给中间部分设置文字和字体颜色
    public void setTitle(String title) {
        setTitle(title, 0);
    }

    //给中间部分设置文字和字体颜色
    public void setTitle(String title, int textColor) {
        tvTitle.setVisibility(View.VISIBLE);
        centerImage.setVisibility(View.GONE);

        tvTitle.setText(title);
        if (textColor != 0){
            tvTitle.setTextColor(getResources().getColor(textColor));
        }
    }

    /**
     * 给中间的图片控件设置图片资源
     * @param src 设置本地的资源文件 没有就传0
     * @param netUrl 设置网络的图片文件 没有就传null或""
     * */
    public void setCenterImage(Context context,int src, String netUrl){
        centerImage.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.GONE);

        if (src != 0){
            ImageManager.getInstance().loadImage(context, centerImage, src, ImageManager.ImageType.CIRCLE);
        }
        if (!TextUtils.isEmpty(netUrl)){
            ImageManager.getInstance().loadImage(context, centerImage, netUrl, ImageManager.ImageType.CIRCLE);
        }
    }

    //给标题的中间部分设置View
    public void setCentreView(View view) {
        if (view == null)
            return;
        centreView.removeAllViews();
        centreView.addView(view);
    }

    /**
     * 给标题的右边button设置文字 图片 点击事件
     * @param index 标记第几个button
     * @param text 文字信息
     * @param src 图片信息 不设置图片src传0
     * @param listener 点击事件
     * */
    public void setBTIndexRes(int index,String text,int src, OnClickListener listener) {
        if (imageRight.getVisibility() == VISIBLE) {
            imageRight.setVisibility(GONE);
        }
        switch (index){
            case 1:
                btRight1.setVisibility(View.VISIBLE);
                btRight2.setVisibility(View.GONE);
                btRight1.setText(text);
                if (src !=0){
                    btRight1.setBackgroundResource(src);
                }
                btRight1.setOnClickListener(listener);
                break;
            case 2:
                btRight2.setVisibility(View.VISIBLE);
                btRight1.setVisibility(View.GONE);
                btRight2.setText(text);
                if (src !=0){
                    btRight2.setBackgroundResource(src);
                }
                btRight2.setOnClickListener(listener);
                break;
        }

    }
    /**
     * 右边的TextView设置资源
     * */
    public void setRightText(String text, OnClickListener listener){
        rightTV.setVisibility(View.VISIBLE);
        rightTV.setText(text);
        rightTV.setOnClickListener(listener);

    }
    //返回右边的TextView控件
    public TextView getRightTV(){
        return rightTV;
    }

    /**
     * 左边的imageView设置资源
     * */
    public void setImageLeft(int res, int width, OnClickListener listener) {
        leftView.setVisibility(GONE);
        leftTextView.setVisibility(GONE);

        if (res >0){
            imageLeft_iv.setImageResource(res);
            imageLeft_iv.setMaxWidth(AutoUtils.getPercentWidthSize(width));
            imageLeft_iv.setVisibility(View.VISIBLE);
            if (listener != null){
                rl_left.setOnClickListener(listener);
            }
        }else {
            imageLeft_iv.setVisibility(View.GONE);
        }

    }

    /**
     * 右边的imageView设置资源
     * */
    public void setImageRight(int res, OnClickListener listener) {
        btRight1.setVisibility(View.GONE);
        btRight2.setVisibility(View.GONE);

        if (res >0){
            imageRight.setImageResource(res);
            imageRight.setMaxWidth(AutoUtils.getPercentWidthSize(96));
            imageRight.setVisibility(View.VISIBLE);
            if (listener != null){
                imageRight.setOnClickListener(listener);
            }
        }else {
            imageRight.setVisibility(View.GONE);
        }

    }
    /**
     * 隐藏右边的imageView
     * */
    public void setImageRightGone(){
        imageRight.setVisibility(GONE);
    }

    /**
     * 右边的imageView设置资源 针对更多操作的图片尺寸 宽高
     * */
    public void setImageRightWH(int width,  int res, OnClickListener listener) {
        btRight1.setVisibility(View.GONE);
        btRight2.setVisibility(View.GONE);

        imageRight.setMaxWidth(AutoUtils.getPercentWidthSize(width));
        imageRight.setImageResource(res);
        imageRight.setVisibility(View.VISIBLE);
        imageRight.setOnClickListener(listener);
    }

    /**
     * 显示未读红点
     * */
    public void showOrHideRightDot(int visible){
        if (msg_noread != null){
            msg_noread.setVisibility(visible);
        }
    }

}
